package cp3120.model;

import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

/**
 * A Model of a User.
 *
 * @author Adam Scarr
 * @author Rowan Spence
 */
public class User {
    private int userID;
    private String username = null;
    private Password password = null;
    private String firstName = null;
    private String lastName = null;
    private String homeAddress = null;
    private String mailingAddress = null;
    private String contactPhone = null;
    private String contactEmail = null;
    private List<Role> roles = new LinkedList<Role>();

    /**
     * Class constructor to create a new empty user.
     */
    public User() {

    }

    public boolean hasPermission(String permission) {
        synchronized (roles) {
            for (Role role: roles) {
                if (role.hasPermission(permission)) {
                    return true;
                }
            }
        }

        return false;
    }

    // ----- getters -----

    /**
     * Simple Getter
     * @return The users username.
     */
    public String getUsername() {
        return username;
    }

    /**
     * Simple Getter
     * @return The users Password
     */
    public Password getPassword() {
        return password;
    }

    /**
     * Simple Getter
     * @return The users home address.
     */
    public String getHomeAddress() {
        return homeAddress;
    }

    /**
     * Simple Getter
     * @return The users mailing address
     */
    public String getMailingAddress() {
        return mailingAddress;
    }

    /**
     * Simple Getter
     * @return The users Phone number.
     */
    public String getContactPhone() {
        return contactPhone;
    }

    /**
     * Simple Getter
     * @return The users email address.
     */
    public String getContactEmail() {
        return contactEmail;
    }

    /**
     * Simple Getter
     * @return The users First Name.
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Simple Getter
     * @return The users last name.
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Simple Getter
     * @return The users unique identifier
     */
    public int getUserID() {
        return userID;
    }

    // ----- setters -----

    /**
     * Simple Setter
     * @param username The users username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Simple Setter
     * @param password The users password.
     */
    public void setPassword(Password password) {
        this.password = password;
    }

    /**
     * Simple Setter
     * @param firstName The users first name.
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Simple Setter
     * @param lastName The users last name.
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * Simple Setter
     * @param userID The users ID. Carefull.
     */
    public void setUserID(int userID) {
        this.userID = userID;
    }

    /**
     * Simple Setter
     * @param homeAddress The users home address.
     */
    public void setHomeAddress(String homeAddress) {
        this.homeAddress = homeAddress;
    }

    /**
     * Simple Setter
     * @param mailingAddress The users mailing address.
     */
    public void setMailingAddress(String mailingAddress) {
        this.mailingAddress = mailingAddress;
    }

    /**
     * Simple Setter
     * @param contactPhone The users contact phone.
     */
    public void setContactPhone(String contactPhone) {
        this.contactPhone = contactPhone;
    }

    /**
     * Simple Setter
     * @param contactEmail The users contact email.
     */
    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        synchronized (roles) {
            this.roles = roles;
        }
    }

    public String toString() {
        return username;
    }

    public User clone() {
        User clone = new User();

        clone.userID = userID;
        clone.username = username;
        clone.password = password;
        clone.firstName = firstName;
        clone.lastName = lastName;
        clone.homeAddress = homeAddress;
        clone.mailingAddress = mailingAddress;
        clone.contactPhone = contactPhone;
        clone.contactEmail = contactEmail;
        clone.roles = new Vector(roles);

        return clone;
    }

    public boolean equals(Object o) {
        if (o instanceof User) {
            return userID == ((User)o).userID;
        }
        return false;
    }
}
